#!/usr/bin/perl

use lib::abs qw( .. ../../base_lib );
use JIP::ToolSet;

use Readonly qw( Readonly );
use English qw( -no_match_vars );
use Carp qw( croak );
use Test::More tests => 13;

Readonly my $OUTPUT_FILE => './output_file.test';

BEGIN { use_ok 'OutputWriter'; }
require_ok 'OutputWriter';

can_ok 'OutputWriter', qw( new init binmode _outputFile _outputHandle _write );

{
    my $out = OutputWriter->new( output_file => $OUTPUT_FILE );

    isa_ok $out, 'OutputWriter';
    isa_ok $out, 'Class::Base';
    isa_ok $out, 'Class::Accessor::Fast';

    isa_ok $out->_outputHandle, 'IO::File';

    is( $out->_outputFile, $OUTPUT_FILE, '_outputFile accessor' );

    try {
        $out->_outputHandle = 'tratata';
    }
    catch {
        like(
            $ARG,
            qr/^Can't modify non-lvalue subroutine call at/,
            'r.o. access to _outputHandle'
        );
    };

    try {
        $out->_outputFile = 'tratata';
    }
    catch {
        like(
            $ARG,
            qr/^Can't modify non-lvalue subroutine call at/,
            'r.o. access to _outputFile'
        );
    };

    try {
        $out->binmode();
    }
    catch {
        like(
            $ARG,
            qr/^required parameter not defined at/,
            'binmode() without required parameters'
        );
    };

    unlink( $OUTPUT_FILE ) or croak 'cant unlink'. $OS_ERROR;
}

{
    try {
        OutputWriter->new;
    }
    catch {
        like(
            $ARG,
            qr/^cannot init mandatory option "output_file" at/,
            'new() without required named parameters'
        );
    };

    try {
        OutputWriter->new( output_file => '/' );
    }
    catch {
        like(
            $ARG,
            qr/^cannot open output file:/,
            'new() with incorrect output file locations'
        );
    };
}
